<template>
	<view class="box" :style="viewColor"> 
		<!-- 搜索 -->
		<view class="search">
			<view class="input">
				<input v-model="searchContent" placeholder="搜索公司行业或公司名称"/>
				<image :src="imgHost+'search.png'"></image>
				<button @click="search">搜索</button>
			</view>
			<view class="searchScreen" v-if="searchScreenShow">
				<view>
					<picker :range="searchScreenClass" range-key="category_name" :value="searchScreenClassIndex" @change="searchScreenClassChange">
						<text class="uni-input">{{searchScreenClass[searchScreenClassIndex].category_name}}</text>
					</picker>
					<image :src="imgHost+'down.png'"></image>
				</view>
				<view>
					<picker :range="searchTimeClass" range-key="title" :value="searchTimeClassIndex" @change="searchTimeClassChange">
						<text class="uni-input">{{searchTimeClass[searchTimeClassIndex].title}}</text>
					</picker>
					<image :src="imgHost+'down.png'"></image>
				</view>
			</view>
			<view class="historySearchTop">
				<view>历史搜索记录</view>
				<view @click="clearHistorySearch">清空</view>
			</view>
			<view class="historySearch" v-if="historySearchList.length>0">
				<view v-for="item in historySearchList" @click="searchContent=item;search()">{{item}}</view>
			</view>
		</view>
		<!-- 有内容 -->
		<view class="contentData" v-if="cardList.length>0">
			<view v-for="item in cardList" @click="toStaffCard(item)">
				<image :src="item.logo"></image>
				<view>
					<view class="left">
						<view>
							<image :src="imgHost+'enterprise.png'"></image>
							<text>{{item.name}}</text>
						</view>
						<view>{{item.type_name}}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 无内容 -->
		<view class="contentEmpty" v-else>
			<image :src="imgHost+'contentEmpty.png'"></image>
		</view>
	</view>
</template>

<script>
	import {
	     HTTP_REQUEST_URL
	} from '@/config/app.js';
	import {
		merClassifly
	} from "@/api/store.js"
	import {
		searchEnterprise,
		deleteCard,
	} from "@/api/userminp.js"
	import { mapGetters } from "vuex";
	export default {
		data() {
			return {
				// 图片路径拼接
				imgHost:HTTP_REQUEST_URL + '/static/contactsCircle/',
				// 筛选行业
				industry:"",
				// 搜索关键字
				searchContent:"",
				// 类型筛选列表
				searchScreenClass:[],
				// 选中类型筛选列表下标
				searchScreenClassIndex:null,
				// 时间筛选列表
				searchTimeClass:[
					{
						title:"最新",
						id:1,
					},
					{
						title:"人气",
						id:2,
					},
				],
				// 选中时间筛选列表下下标
				searchTimeClassIndex:0,
				// 筛选显示条件
				searchScreenShow : false,
				// 历史搜索记录
				historySearchList:[],
				// 搜索到名片列表
				cardList:[],
				// 页数
				page:1,
			};
		},
		computed: {
			...mapGetters(['viewColor'])
		},
		onLoad(optive) {
			this.merClassifly()
			if(optive.name){
				this.industry= optive.name
				this.searchScreenShow = true
			}
			if(uni.getStorageSync("peopleSearch")){
				this.historySearchList=uni.getStorageSync("peopleSearch")
				console.log(this.historySearchList)
			}
		},
		onShow() {
			this.page=1
			this.cardList=[]
			this.searchEnterprise()
		},
		watch:{
			searchScreenClassIndex:function(){
				this.page=1
				this.cardList=[]
				this.searchEnterprise()
			},
			searchTimeClassIndex:function(){
				this.page=1
				this.cardList=[]
				this.searchEnterprise()
			}
		},
		methods:{
			// 跳转员工列表
			toStaffCard(item){
				uni.navigateTo({
					url:"/pages/contactsCircle/staffCardList/staffCardList?id="+item.id
				})
			},
			// 操作名片
			operation(item,index){
				if(item.client_check==1){
					deleteCard({
						del_id:item.uid
					}).then(res=>{
						uni.showToast({
							title:res.message
						})
						this.cardList[index].client_check=0;
					}).catch(err=>{
						uni.showToast({
							title:err
						})
					})
				}else{
					uni.navigateTo({
						url:"/pages/userminp/mine/tianjia/tianjia?share="+item.uid
					})
				}
			},
			// 清空历史搜索记录
			clearHistorySearch(){
				this.historySearchList=[]
				uni.removeStorageSync("peopleSearch")
			},
			// 搜索
			search(){
				if(this.searchContent){
					var arr = [];
					if(uni.getStorageSync("peopleSearch")){
						arr = uni.getStorageSync("peopleSearch")
						arr.unshift(this.searchContent)
					}else{
						arr.unshift(this.searchContent)
					}
					this.historySearchList.unshift(this.searchContent)
					uni.setStorageSync("peopleSearch",arr)
				}
				this.page=1
				this.cardList=[]
				this.searchEnterprise()
			},
			// 获取名片列表
			searchEnterprise(){
				searchEnterprise({
					name:this.searchContent,
					type:this.searchScreenShow ? this.searchScreenClass[this.searchScreenClassIndex].merchant_category_id : '',
					order_type:this.searchScreenShow ? this.searchTimeClass[this.searchTimeClassIndex].id : '',
					page:this.page
				}).then(res=>{
					for(var i=0;i<res.data.length;i++){
						this.cardList.push(res.data[i]) 
					}
				})
			},
			// 获取企业行业列表
			async merClassifly(){
				await merClassifly().then(res=>{
					this.searchScreenClass = res.data
					for(var i=0;i<this.searchScreenClass.length;i++){
						if(this.searchScreenClass[i].category_name == this.industry){
							this.searchScreenClassIndex=i
						}
					}
				})
			},
			// 选择类型筛选列表
			searchScreenClassChange: function(e) {
				this.searchScreenClassIndex = e.detail.value
			},
			// 选择时间筛选列表
			searchTimeClassChange: function(e) {
				this.searchTimeClassIndex = e.detail.value
			},
		},
		onReachBottom() {
			this.page++;
			this.searchEnterprise()
		}
	}
</script>
<style>
	page {
		background-color: white;
	}
</style>
<style lang="scss" scoped>
	
	.box{
		padding-top: 20rpx;
		background-color: white;
		.search{
			// margin: 0 30rpx 30rpx 30rpx;
			.input{
				position: relative;
				background-color: #F2F2F2;
				border-radius: 40rpx;
				padding: 20rpx 20rpx 20rpx 100rpx;
				margin: 0 30rpx 30rpx 30rpx;
				image{
					position: absolute;
					top: 50%;
					left: 38rpx;
					width: 32rpx;
					height: 32rpx;
					transform: translateY(-50%);
				}
				&>button{
					width: 88rpx;
					background-color: var(--view-theme);
					font-size: 28rpx;
					color: white;
					position: absolute;
					right: 20rpx;
					top: 50%;
					transform: translateY(-50%);
					padding: 10rpx;
					border-radius: 30rpx;
					z-index: 100;
				}
			}
			.searchScreen{
				border-top: 2rpx solid #F0F0F0;
				border-bottom: 2rpx solid #F0F0F0;
				font-size: 28rpx;
				padding: 20rpx 30rpx;
				display: flex;
				align-items: center;
				&>view{
					display: flex;
					align-items: center;
					width: 200rpx;
				}
				image{
					width: 32rpx;
					height: 32rpx;
				}
			}
			.historySearchTop{
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 30rpx;
				&>view:nth-of-type(1){
					font-size: 34rpx;
				}
				&>view:nth-of-type(2){
					color: #666666;
					font-size: 28rpx;
				}
			}
			.historySearch{
				margin: 10rpx 0;
				display: flex;
				flex-wrap: wrap;
				margin: 0 30rpx 30rpx 30rpx;
				&>view{
					padding: 15rpx;
					border-radius: 20rpx;
					margin: 10rpx 10rpx 10rpx 0;
					background-color: #EEEEEE;
					color: #666666;
				}
			}
		}
		.contentData{
			// margin-top:20rpx;
			padding: 0 30rpx 30rpx 30rpx;
			&>view{
				border-bottom: 2rpx solid #F0F0F0;
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				&>image{
					border-radius: 10rpx;
					width: 156rpx;
					height: 156rpx;
				}
				&>view{
					// width: 70%;
					margin-left: 20rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.left{
						&>view:nth-of-type(1){
							display: flex;
							align-items: center;
							margin-bottom: 20rpx;
							text{
								font-size: 34rpx;
								font-weight: 600;
							}
							image{
								height: 42rpx;
								width: 40rpx;
								margin-right: 10rpx;
							}
						}
						&>view:nth-of-type(2){
							color: #999999;
							font-size: 28rpx;
						}
					}
					.right{
						text-align: center;
						image{
							width: 36rpx;
							height: 36rpx;
						}
					}
				}
			}
			&>view:last-of-type{
				border-bottom: none;
			}
		}
		.contentEmpty{
			text-align: center;
		}
	}
</style>
